<?php

class User_Model_User extends Zend_Db_Table_Row_Abstract
{

    /**
     * get user id
     */
    public function getId()
    {
        return isset($this -> user_id) ? $this -> user_id : 0;
    }

    public function getRoleId()
    {
        return $this -> role_id ? $this -> role_id : 8;
    }

    /**
     * @return string
     */
    public function getGlobalType()
    {
        return 'user';
    }

    /*
     * @return string
     */
    public function getGlobalId()
    {
        return $this -> getGlobalType() . '.' . $this -> getId();
    }

    public function getHref()
    {
        return Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array('id' => $this -> getId()), 'user_profile', true);
    }

    public function getTitle()
    {
        if (!empty($this -> displayname))
        {
            return $this -> displayname;
        }
        if (!empty($this -> username))
        {
            return $this -> username;
        }
        return $this -> getId();
    }

    public function __toString()
    {
        return sprintf('<a href="%s" class="%s">%s</a>', $this -> getHref(), 'user', $this -> getTitle());
    }

    /**
     * update password
     * @param string $string
     * @return User_Model_User
     */
    public function setPassword($password)
    {
        $this -> salt = Nuo_Utility_String::createRandomString(8, 1);
        $this -> password = $this -> hashPassword($password, $this -> salt);
        return $this;
    }

    /**
     * @param string $password
     * @param string $salt
     * @return string
     */
    public function hashPassword($password, $salt)
    {
        return sha1($password . $salt, false);
    }

    /**
     * check passord is valid
     */
    public function isValidPassword($password)
    {
        return $this -> password === $this -> hashPassword($password, $this -> salt);
    }

}
